package com.hospital.diagnosis.module.config;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.hospital.diagnosis.module.utils.ai.tools.DiagnosisRecordTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AI聊天客户端配置
 */
@Configuration
public class ChatClientConfig {
    @Autowired
    private ChatClient.Builder builder;
    @Autowired
    private DocumentRetriever documentRetriever;
    @Autowired
    private MessageChatMemoryAdvisor memoryAdvisor;
    @Autowired
    private DiagnosisRecordTools diagnosisRecordTools;
    @Value("${chatbot.system-prompt}")
    private String systemPrompt;

    @Bean
    public ChatClient chatClient() {
        return builder
                .defaultAdvisors(
                        new DocumentRetrievalAdvisor(documentRetriever), // 文档检索
                        new SimpleLoggerAdvisor(),  // 日志记录
                        memoryAdvisor // 聊天记忆
                )
                .defaultTools(diagnosisRecordTools) // 添加自定义工具
                .defaultSystem(systemPrompt)
                .build();
    }
}
